package edu.gettysburg.catapp1;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

/** 
 * Path Overlay Class 
 * @author Patrice Bramble 
 * Used to draw a path on a mapview 
 */ 

public class PathOverlay extends Overlay{

	private int color; 
	private ArrayList<GeoPoint> points, sitePoints;
	private Path path;
	private Path path2;
	private Projection projection; 

	public PathOverlay(ArrayList<GeoPoint> points, ArrayList<GeoPoint> sitePoints, MapView v){ 
		this.color = color; 
		this.points = points; 
		this.sitePoints = sitePoints;
		path = new Path(); 
		path2 = new Path(); 
		projection = v.getProjection();
	}


	public void draw(Canvas canvas, MapView mapv, boolean shadow){ 
		super.draw(canvas, mapv, shadow); 
		//Configuring the paint brush 
		Paint mPaint = new Paint(); 
		mPaint.setDither(true); 
		mPaint.setColor(Color.BLUE); 
		mPaint.setStyle(Paint.Style.FILL_AND_STROKE); 
		mPaint.setStrokeJoin(Paint.Join.ROUND); 
		mPaint.setStrokeCap(Paint.Cap.ROUND); 
		mPaint.setStrokeWidth(4); 

		path = new Path(); 
		path2 = new Path(); 

		for(int i = 0; i < points.size(); i++){ 
			GeoPoint gp1 = points.get(i); 
			Point p1 = new Point(); 
			projection.toPixels(gp1,p1); 

			if(i <= points.size() - 2){ 
				GeoPoint gp2 = points.get(i+1); 
				Point p2 = new Point(); 
				projection.toPixels(gp2,p2); 
				path.moveTo(p1.x, p1.y); 
				path.lineTo(p2.x,p2.y); 
			}     
		} 
		Paint m2Paint = new Paint(); 
		m2Paint.setDither(true); 
		m2Paint.setColor(Color.RED); 
		m2Paint.setStyle(Paint.Style.FILL_AND_STROKE); 
		m2Paint.setStrokeJoin(Paint.Join.ROUND); 
		m2Paint.setStrokeCap(Paint.Cap.ROUND); 
		m2Paint.setStrokeWidth(4); 

		for(int i = 0; i < points.size(); i++) {
			if(points.get(i).equals(sitePoints.get(InitialSite.SITE_NUM-1))) {
				for(int j = 0; j < points.size()-i-1; j++) {
					if(!points.get(i+j).equals(sitePoints.get(InitialSite.SITE_NUM))) {
						GeoPoint gp1 = points.get(i+j); 
						Point p1 = new Point(); 
						projection.toPixels(gp1,p1); 

						GeoPoint gp2 = points.get(i+j+1); 
						Point p2 = new Point(); 
						projection.toPixels(gp2,p2); 
						path2.moveTo(p1.x, p1.y); 
						path2.lineTo(p2.x,p2.y);     
					}
					else {
						break;
					}
				}
				break;
			}
		} 

		canvas.drawPath(path, mPaint); 
		canvas.drawPath(path2, m2Paint); 

	} 


} 


